package com.jiang.readline.Utils;

import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;

@Component
public class CountCommentsUtils {

    public static int CommentsLines(String filePath) {
        int count = 0;
        boolean inComment = false; // 是否在注释中
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
               if(inComment){
                   if(!line.isEmpty()) count++;
                   if (line.endsWith("*/")) {     //注释结尾，false
                       inComment = false;
                   }
               } else {
                if (line.startsWith("/*")) {    //注释开始，开始++
                    inComment = true;
                    count++;
                } else if (line.startsWith("//")) {
                    count++;
                }
            }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }

    public static int CommentsLinesForRuby(String filePath) {
        int count = 0;
        boolean inComment = false; // 是否在注释中
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.trim();
                if(inComment){
                    if(!line.isEmpty()) count++;
                    if (line.endsWith("=end")) {
                        inComment = false;
                    }
                } else {
                    if (line.startsWith("=begin")) {
                        inComment = true;
                        count++;
                    } else if (line.startsWith("##")) {
                        count++;
                    }
                }


            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }
}
